﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Collections;


namespace IntelliTouchMix
{
    public class TouchController
    {
        frmMain formMain;
        BlobController bc;
        UIController uic;
        int lastPanel;

        public TouchController(frmMain formMain)
        {
            this.formMain = formMain;
            uic = new UIController(formMain);
            bc = new BlobController(16, 130);
            lastPanel = Util.NONE;

            ControlSense(Util.selectedPanel);

            bc.VerticalDownRightHandler += new Gesture(bc_VerticalDownRightHandler);
            bc.VerticalUpLeftHandler += new Gesture(bc_VerticalUpLeftHandler);
            bc.VerticalDownLeftHandler += new Gesture(bc_VerticalDownLeftHandler);
            bc.VerticalUpRightHandler +=new Gesture(bc_VerticalUpRightHandler);
            bc.BigVerticalUpRightHandler += new Gesture(bc_BigVerticalUpRightHandler);
            bc.BigVerticalDownLeftHandler += new Gesture(bc_BigVerticalDownLeftHandler);
            bc.BigVerticalUpLeftHandler += new Gesture(bc_BigVerticalUpLeftHandler);
            bc.BigVerticalDownRightHandler += new Gesture(bc_BigVerticalDownRightHandler);
            bc.TwoFingersDownHandler += new Gesture(bc_TwoFingersDownHandler);
            bc.TwoFingersUpHandler += new Gesture(bc_TwoFingersUpHandler);
            bc.ThreeFingersDownHandler += new Gesture(bc_ThreeFingersDownHandler);
            bc.ThreeFingersUpHandler += new Gesture(bc_ThreeFingersUpHandler);
            bc.GestureHandler += new Gesture(bc_GestureHandler);
            bc.LDownRight += new Gesture(bc_LDownRight);
            bc.LUpRight += new Gesture(bc_LUpRight);
            bc.LUpLeft += new Gesture(bc_LUpLeft);
            bc.LDownLeft += new Gesture(bc_LDownLeft);
            bc.LLeftDown += new Gesture(bc_LLeftDown);
            bc.LRightDown += new Gesture(bc_LRightDown);
            bc.LLeftUp += new Gesture(bc_LLeftUp);
            bc.LRightUp += new Gesture(bc_LRightUp);
            bc.UInvertedPositiveHandler += new Gesture(bc_UInvertedPositiveHandler);
            bc.UInvertedNegativeHandler += new Gesture(bc_UInvertedNegativeHandler);

            Microsoft.Research.TouchMouseSensor.TouchMouseSensorEventManager.Handler += this.TouchMouseSensorHandler;
        }

        void selectControl()
        {
            formMain.Invoke((MethodInvoker)delegate
            {
                try
                {
                    Util.controllContourPosition[Util.selectedPanel][Util.selectedControl[Util.selectedPanel]].Select();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.StackTrace);
                }
            });
        }

        void bc_VerticalDownLeftHandler(object sender, TouchMouseEventArgs e)
        {
            formMain.formMain.Invoke((MethodInvoker)delegate
            {
                //formMain.formMain.playFadeSongs();
                formMain.formMain.playGesture(false);
            });
        }

        void bc_VerticalDownRightHandler(object sender, TouchMouseEventArgs e)
        {
            formMain.formMain.Invoke((MethodInvoker)delegate
            {
                formMain.formMain.playSample(formMain.formMain.SAMPLE2_PLAYER);
            });
        }
        
        void bc_UInvertedNegativeHandler(object sender, TouchMouseEventArgs e)
        {
            if (Util.snapStatus == Util.SNAP_ON)
            {
                Util.snapStatus = Util.SNAP_OFF;
            }
            else
            {
                if (Util.snapValue[Util.snapStatus] == Util.SNAP_OFF_0_5)
                {
                    Util.snapValue[Util.snapStatus] = Util.SNAP_OFF_1;
                }
                else if (Util.snapValue[Util.snapStatus] == Util.SNAP_OFF_1)
                {
                    Util.snapValue[Util.snapStatus] = Util.SNAP_OFF_2;
                }
                else if (Util.snapValue[Util.snapStatus] == Util.SNAP_OFF_2)
                {
                    Util.snapValue[Util.snapStatus] = Util.SNAP_OFF_0_5;
                }
            }
        }

        void bc_UInvertedPositiveHandler(object sender, TouchMouseEventArgs e)
        {
            if (Util.snapStatus == Util.SNAP_OFF)
            {
                Util.snapStatus = Util.SNAP_ON;
            }
            else
            {
                if (Util.snapValue[Util.snapStatus] == Util.SNAP_ON_BEAT)
                {
                    Util.snapValue[Util.snapStatus] = Util.SNAP_ON_4;
                }
                else if(Util.snapValue[Util.snapStatus] == Util.SNAP_ON_4)
                {
                    Util.snapValue[Util.snapStatus] = Util.SNAP_ON_8;
                }
                else if (Util.snapValue[Util.snapStatus] == Util.SNAP_ON_8)
                {
                    Util.snapValue[Util.snapStatus] = Util.SNAP_ON_16;
                }
                else if (Util.snapValue[Util.snapStatus] == Util.SNAP_ON_16)
                {
                    Util.snapValue[Util.snapStatus] = Util.SNAP_ON_BEAT;
                }
            }
        }

        public void ControlSense(int panelSelected)
        {
            Gesture ControlChange = new Gesture(bc_ChangeControlHandler);
            Gesture ValueChange = new Gesture(bc_ChangeValueHandler);
            int sense = Util.panelControllingType[panelSelected];
            if (lastPanel != Util.NONE)
            {
                if (Util.panelControllingType[lastPanel] != sense)
                {
                    if (sense == Util.CHANGE_CONTROL_WITH_LEFT_RIGHT)
                    {
                        bc.OneFingerDownHandler -= ControlChange;
                        bc.OneFingerUpHandler -= ControlChange;
                        bc.OneFingerDownRealTimeHandler -= ControlChange;
                        bc.OneFingerUpRealTimeHandler -= ControlChange;
                        bc.OneFingerLeftHandler -= ValueChange;
                        bc.OneFingerRightHandler -= ValueChange;
                        bc.OneFingerLeftRealTimeHandler -= ValueChange;
                        bc.OneFingerRightRealTimeHandler -= ValueChange;
                    }
                    else
                    {
                        bc.OneFingerDownHandler -= ValueChange;
                        bc.OneFingerUpHandler -= ValueChange;
                        bc.OneFingerDownRealTimeHandler -= ValueChange;
                        bc.OneFingerUpRealTimeHandler -= ValueChange;
                        bc.OneFingerLeftHandler -= ControlChange;
                        bc.OneFingerRightHandler -= ControlChange;
                        bc.OneFingerLeftRealTimeHandler -= ControlChange;
                        bc.OneFingerRightRealTimeHandler -= ControlChange;
                    }
                }
                else
                {
                    lastPanel = panelSelected;
                    return;
                }
            }
            if (sense == Util.CHANGE_CONTROL_WITH_LEFT_RIGHT)
            {
                bc.OneFingerDownHandler += ValueChange;
                bc.OneFingerUpHandler += ValueChange;
                bc.OneFingerDownRealTimeHandler += ValueChange;
                bc.OneFingerUpRealTimeHandler += ValueChange;
                bc.OneFingerLeftHandler += ControlChange;
                bc.OneFingerRightHandler += ControlChange;
                bc.OneFingerLeftRealTimeHandler += ControlChange;
                bc.OneFingerRightRealTimeHandler += ControlChange;
            }
            else
            {
                bc.OneFingerDownHandler += ControlChange;
                bc.OneFingerUpHandler += ControlChange;
                bc.OneFingerDownRealTimeHandler += ControlChange;
                bc.OneFingerUpRealTimeHandler += ControlChange;
                bc.OneFingerLeftHandler += ValueChange;
                bc.OneFingerRightHandler += ValueChange;
                bc.OneFingerLeftRealTimeHandler += ValueChange;
                bc.OneFingerRightRealTimeHandler += ValueChange;
            }
            lastPanel = panelSelected;
        }

        public void bc_LRightUp(object sender, TouchMouseEventArgs e)
        { Util.centerForm.Invoke((MethodInvoker)delegate
            {
                Util.centerForm.maximize4();
            });
            Util.selectedPanel = Util.LIBRARY;
            Util.zoomedPanel = Util.zoomedPanel == Util.LIBRARY ? Util.NONE : Util.LIBRARY;
            ControlSense(Util.selectedPanel);
           
        }

        public void bc_LLeftUp(object sender, TouchMouseEventArgs e)
        { 
            Util.centerForm.Invoke((MethodInvoker)delegate
            {
                Util.centerForm.maximize3();

            });
            Util.selectedPanel = Util.INDICATORS;
            Util.zoomedPanel = Util.zoomedPanel == Util.INDICATORS ? Util.NONE : Util.INDICATORS;
            ControlSense(Util.selectedPanel);
           
        }

        public void bc_LRightDown(object sender, TouchMouseEventArgs e)
        { 
            Util.centerForm.Invoke((MethodInvoker)delegate
            {

                Util.centerForm.maximize2();
            });
            Util.selectedPanel = Util.DECK_B;
            Util.zoomedPanel = Util.zoomedPanel == Util.DECK_B ? Util.NONE : Util.DECK_B;
            ControlSense(Util.selectedPanel);
           

        }

        public void bc_LLeftDown(object sender, TouchMouseEventArgs e)
        {
            Util.selectedPanel = Util.DECK_A;
            Util.zoomedPanel = Util.zoomedPanel==Util.DECK_A?Util.NONE:Util.DECK_A;
            ControlSense(Util.selectedPanel);
            Console.WriteLine("Maximize Panel:" + Util.selectedPanel);
            Util.centerForm.Invoke((MethodInvoker)delegate
            {
                Util.centerForm.maximize1();
                
            });
            /*
            Util.deckA.Invoke((MethodInvoker)delegate
            {
                Util.centerForm.expand1 = Util.DECK_A == Util.zoomedPanel;
                Util.centerForm.minimize1 = !(Util.DECK_A == Util.zoomedPanel);
                Util.centerForm.expand2 = Util.DECK_B == Util.zoomedPanel;
                Util.centerForm.minimize2 = !(Util.DECK_B == Util.zoomedPanel);
                Util.centerForm.expand3 = Util.INDICATORS == Util.zoomedPanel;
                Util.centerForm.minimize3 = !(Util.INDICATORS == Util.zoomedPanel);
                Util.centerForm.expand4 = Util.MONITOR_PLAYER == Util.zoomedPanel;
                Util.centerForm.minimize4 = !(Util.MONITOR_PLAYER == Util.zoomedPanel);
            });*/
        }

        public void bc_LDownLeft(object sender, TouchMouseEventArgs e)
        {  
            Util.playList.Invoke((MethodInvoker)delegate
            {
                Util.deSelectAll();
                Util.playList.panelPlayList.ColorSchemeStyle = DevComponents.DotNetBar.eDotNetBarStyle.Office2000;
                Util.playList.panelPlayList.Refresh();
            });
            Util.selectedPanel = Util.LIBRARY;
            Console.WriteLine("Selected Panel:" + Util.selectedPanel);
            ControlSense(Util.selectedPanel);
        }

        public void bc_LUpLeft(object sender, TouchMouseEventArgs e)
        {
            Util.selectedPanel = Util.DECK_B;
            Console.WriteLine("Selected Panel:" + Util.selectedPanel);
            ControlSense(Util.selectedPanel);
            Util.deckB.Invoke((MethodInvoker)delegate
            {
                Util.deSelectAll();
                Util.deckB.panelEx1.ColorSchemeStyle = DevComponents.DotNetBar.eDotNetBarStyle.Office2000;
                Util.deckB.panelEx1.Refresh();
            });
        }
        
        public void bc_LUpRight(object sender, TouchMouseEventArgs e)
        {
            Util.selectedPanel = Util.DECK_A;
            Util.deckA.Invoke((MethodInvoker)delegate{
                Util.deSelectAll();
                Util.deckA.panelEx1.ColorSchemeStyle = DevComponents.DotNetBar.eDotNetBarStyle.Office2000;
                Util.deckA.panelEx1.Refresh();
            });
            Console.WriteLine("Selected Panel:" + Util.selectedPanel);
            ControlSense(Util.selectedPanel);
        }

        public void bc_LDownRight(object sender, TouchMouseEventArgs e)
        {
            Util.selectedPanel = Util.INDICATORS;
            Console.WriteLine("Selected Panel:" + Util.selectedPanel);
            Util.indicatorForm.Invoke((MethodInvoker)delegate
            {
                Util.deSelectAll();
                Util.indicatorForm.selectPanel();
            });
            ControlSense(Util.selectedPanel);
        }

        public void bc_ThreeFingersUpHandler(object sender, TouchMouseEventArgs e)
        {
            formMain.formMain.Invoke((MethodInvoker)delegate{
            uic.controllTrackBar(formMain.formMain.axGSlider1, e, 10);
            formMain.formMain.axGSlider1.Refresh();
            });
        }

        public void bc_ThreeFingersDownHandler(object sender, TouchMouseEventArgs e)
        {
            formMain.formMain.Invoke((MethodInvoker)delegate{
            uic.controllTrackBar(formMain.formMain.axGSlider1, e, 10);
            formMain.formMain.axGSlider1.Update();
            });
        }

        public void bc_TwoFingersUpHandler(object sender, TouchMouseEventArgs e)
        {
            formMain.formMain.Invoke((MethodInvoker)delegate{
                AxSLIDERLib.AxGSlider temp = formMain.formMain.getPitchTrackbar();
                if(temp!=null)
                {
                    uic.controllTrackBar(temp, e, 10);
                    temp.Update();
                }
            });
        }

        public void bc_TwoFingersDownHandler(object sender, TouchMouseEventArgs e)
        {
            formMain.formMain.Invoke((MethodInvoker)delegate
            {
                /*
                int player = formMain.formMain.playerPlaying();
                if (player == 1)
                {
                    uic.controllTrackBar(formMain.formMain.GSlider_Tempo1, e, 10);
                    formMain.formMain.GSlider_Tempo1_Slide(new object(), EventArgs.Empty);
                }
                else if (player == 2)
                {
                    uic.controllTrackBar(formMain.formMain.GSlider_Tempo2, e, 10);
                    formMain.formMain.GSlider_Tempo2_Slide(new object(), EventArgs.Empty);
                }
                 * */
            });
        }

        public void bc_VerticalUpRightHandler(object sender, TouchMouseEventArgs e)
        {
            formMain.formMain.Invoke((MethodInvoker)delegate{
                //formMain.formMain.playFadeSongs();
                formMain.formMain.playGesture(true);
            });
        }

        public void bc_BigVerticalDownLeftHandler(object sender, TouchMouseEventArgs e)
        {
            formMain.formMain.Invoke((MethodInvoker)delegate{
                formMain.formMain.loadDeckB();
            });
        }

        public void bc_BigVerticalUpRightHandler(object sender, TouchMouseEventArgs e)
        {
            formMain.formMain.Invoke((MethodInvoker)delegate
            {
                formMain.formMain.loadDeckA();
            });
        }

        public void bc_BigVerticalDownRightHandler(object sender, TouchMouseEventArgs e)
        {
            uic.moveItemListBox(formMain.formMain.PlayList1, e);
        }

        public void bc_BigVerticalUpLeftHandler(object sender, TouchMouseEventArgs e)
        {
            uic.moveItemListBox(formMain.formMain.PlayList1, e);
        }

        public void bc_VerticalUpLeftHandler(object sender, TouchMouseEventArgs e)
        {
            formMain.formMain.Invoke((MethodInvoker)delegate
            {
                formMain.formMain.playSample(formMain.formMain.SAMPLE1_PLAYER);
            });
        }

        public void bc_ChangeControlHandler(object sender, TouchMouseEventArgs e)
        {
            OrderType order = Util.panelControllingType[Util.selectedPanel]==Util.CHANGE_CONTROL_WITH_LEFT_RIGHT?OrderType.ASC:OrderType.DESC;
            e = adjustSense(order, e);
            Util.selectedControl[Util.selectedPanel] = uic.increaseOrDecreaseLinearValue(e, Util.selectedControl[Util.selectedPanel], 0, Util.controllContourPosition[Util.selectedPanel].Length - 1, 1, 1, OrderType.ASC);
            Console.WriteLine("Panel:"+Util.selectedPanel + ",Control:" + Util.selectedControl[Util.selectedPanel]);
            selectControl();
        }

        public void bc_GestureHandler(object sender, TouchMouseEventArgs e)
        {
            
         //   MessageBox.Show("jiji");

            //throw new NotImplementedException();
        }

        TouchMouseEventArgs adjustSense(OrderType order, TouchMouseEventArgs e)
        {
            if (order == OrderType.DESC)
            {
                if (e.senseType == SenseType.NEGATIVE)
                    e.senseType = SenseType.POSITIVE;
                else
                    e.senseType = SenseType.NEGATIVE;
            }
            return e;
        }

        void bc_ChangeValueHandler(object sender, TouchMouseEventArgs e)
        {            
            //OrderType order = Util.panelControllingType[Util.selectedPanel] == Util.CHANGE_CONTROL_WITH_LEFT_RIGHT ? OrderType.DESC : OrderType.ASC;
            //e = adjustSense(order, e);
            if (Util.selectedPanel == Util.DECK_A)
            {
                if (Util.selectedControl[Util.selectedPanel] == Util.DECK_WAVE_SCROLL)
                {
                    uic.controllWaveScroll(Util.PLAYER1, e);
                }
                else if (Util.selectedControl[Util.selectedPanel] == Util.DECK_VOLUME)
                {
                    uic.controllTrackBar(formMain.formMain.GSlider_Vol1, e, 10);
                }
                else if (Util.selectedControl[Util.selectedPanel] == Util.DECK_TEMPO)
                {
                    uic.controllTrackBar(formMain.formMain.GSlider_Tempo1, e, 10);
                }
                else if (Util.selectedControl[Util.selectedPanel] == Util.DECK_RATE)
                {
                    uic.controllTrackBar(formMain.formMain.GSlider_Rate1, e, 10);
                }
            }
            else if(Util.selectedPanel == Util.DECK_B)
            {
                if (Util.selectedControl[Util.selectedPanel] == Util.DECK_VOLUME)
                {
                    uic.controllTrackBar(formMain.formMain.GSlider_Vol2, e, 10);
                }
                else if (Util.selectedControl[Util.selectedPanel] == Util.DECK_TEMPO)
                {
                    uic.controllTrackBar(formMain.formMain.GSlider_Tempo2, e, 10);
                }
                else if (Util.selectedControl[Util.selectedPanel] == Util.DECK_RATE)
                {
                    uic.controllTrackBar(formMain.formMain.GSlider_Rate2, e, 10);
                }
            }
            else if (Util.selectedPanel == Util.LIBRARY)
            {
                if (Util.selectedControl[Util.selectedPanel] == Util.LIBRARY_SELECTED)
                {
                    uic.controllListBox(formMain.formMain.PlayList1, e);
                }
                else if (Util.selectedControl[Util.selectedPanel] == Util.LIBRARY_EFFECT_BAR)
                {
                    uic.controllTrackBar(formMain.formMain.axGSlider1, e,10);
                }
            }
        }

        void TouchMouseSensorHandler(object sender, Microsoft.Research.TouchMouseSensor.TouchMouseSensorEventArgs e)
        {
            // We're in a thread belonging to the mouse, not the user interface 
            // thread. Need to dispatch to the user interface thread.
            //Dispatcher.Invoke((Action<TouchMouseSensorEventArgs>)SetSource, e);
            //Dispatcher.Invoke((Action<TouchMouseSensorEventArgs>)SetSource, e);
            //try
            {
                SetSource(e);
                //Invoke((Action<Microsoft.Research.TouchMouseSensor.TouchMouseSensorEventArgs>)SetSource, e);
            }
            //catch (Exception ex){MessageBox.Show("Mi Errr  "+ex.StackTrace);}
        }

        void SetSource(Microsoft.Research.TouchMouseSensor.TouchMouseSensorEventArgs e)
        {
            int w = e.Status.m_dwImageWidth;
            int h = e.Status.m_dwImageHeight;
            // Dispatcher.Invoke((Action)(Hi));
            bc.actualize(e);

            // out_text.Text = "jojojo\nte quioer";
            // MessageBox.Show("w " + w + " h " + h);
            /*
             BitmapSource source =
                 BitmapSource.Create(e.Status.m_dwImageWidth, e.Status.m_dwImageHeight,
                 96, 96,
                 PixelFormats.Gray8, null, e.Image, e.Status.m_dwImageWidth);
            
             // Blobs[] blobs = bControler.find(e.Image, w, h);

             // Show bitmap in user interface.
             SensorImage.Source = source;
             // Print Blob data
             output.Content = "";
             for (int i = 0; i < bControler.iblob; ++i)
             {
                 output.Content += i + " " + blobs[i].ToString() + "\n";
             }*/
        }
    }
}
